package com.shujia.MapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo05SumScore {
    // Map端
    public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            // 1500100001,1000001,98
            String[] splits = value.toString().split(",");
            String id = splits[0];
            int score = Integer.parseInt(splits[2]);
            // 以学生id作为key，score作为value
            context.write(new Text(id), new IntWritable(score));
        }
    }

    // Combiner 发生在Map端的Reduce
    public static class MyCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            // key 学生id
            // values 每个学生的六门科目成绩
            int sum = 0;// 记录总分
            for (IntWritable score : values) {
                sum += score.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }

    // Reduce端
    public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            // key 学生id
            // values 每个学生的六门科目成绩
            int sum = 0;// 记录总分
            for (IntWritable score : values) {
                sum += score.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }

    // Driver程序，主要负责配置及提交任务
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://master:9000");
        // 创建一个MapReduce的Job
        Job job = Job.getInstance(conf);
        // 配置任务
        // 设置任务运行的名字
        job.setJobName("Demo05SumScore");
        // 设置任务运行哪一个类
        job.setJarByClass(Demo05SumScore.class);


        // 配置Map端
        // 指定Map任务运行哪一个类
        job.setMapperClass(MyMapper.class);
        // 配置Map端输出的Key的类型
        job.setMapOutputKeyClass(Text.class);
        // 配置Map端输出的Value的类型
        job.setMapOutputValueClass(IntWritable.class);

        // 配置Combiner
        job.setCombinerClass(MyCombiner.class);

        // 配置Reduce端
        // 指定Reduce任务运行哪一个类
        job.setReducerClass(MyReducer.class);
        // 配置Reduce端输出的Key的类型
        job.setOutputKeyClass(Text.class);
        // 配置Reduce端输出的Value的类型
        job.setOutputValueClass(IntWritable.class);

        // 配置输入输出路径

        FileInputFormat.addInputPath(job, new Path("/data/score/input"));

        Path path = new Path("/data/sumScore/output");
        FileSystem fs = FileSystem.get(conf);
        // 判断输出路径是否存在，存在即删除
        if (fs.exists(path)) {
            fs.delete(path, true);
        }
        // 输出路径已存在会报错
        FileOutputFormat.setOutputPath(job, path);


        // 等待任务运行完成
        job.waitForCompletion(true);

    }
    /*
        hadoop jar Hadoop-1.0.jar com.shujia.MapReduce.Demo05SumScore
     */
}
